<template>
  <view class="content">
    <cu-custom bgColor="bg-blue" :isBack="false">
    </cu-custom>
    <view style="text-align: center;">
      <image class="logo" src="/static/logo.png"></image>
    </view>
    <view v-if="errorMsg" style="text-align: center;">
      <text class="title">错误信息：{{ errorMsg }}</text>
    </view>
    <view v-else>
      <view style="text-align: center;margin-bottom: 30px;">
        <text style="font-size: 18px;">您正在登录，请确认是否是本人</text>
      </view>
      <uni-section type="line" title="">
        <uni-list>
          <uni-list-item title="登录省份" :rightText="analysisData.province"/>
          <uni-list-item title="登录城市" :rightText="analysisData.city"/>
          <uni-list-item title="登录ip" :rightText="loginData.ip"/>
          <uni-list-item title="登录设备" :rightText="loginData.browser"/>
        </uni-list>
      </uni-section>
      <view style="margin: 20px;margin-top: 40px;">
        <button type="primary" style="border-radius: 30px" @click="confirmLogin">确认登录</button>
        <br>
        <button type="default" style="border-radius: 30px" @click="cancel">取消登录</button>
      </view>
    </view>
  </view>
</template>

<script>
import {
  API_URL
} from "@/env";

export default {
  data() {
    return {
      title: 'Hello',
      loginUid: '',
      errorMsg: '',
      analysisData: {},
      loginData: {},
      userInfo: {}
    }
  },
  async onLoad(options) {
    // 1. 校验是否有login_uid
    if (options.login_uid) {
      this.loginUid = options.login_uid
      this.getScanInfo()
    } else {
      uni.navigateTo({
        url: '/pages/index/index'
      });
    }
  },
  methods: {
    // 获取扫码信息
    getScanInfo() {
      uni.request({
        url: API_URL + 'api/dvadmin_third/login/get_scan_info/', //仅为示例，并非真实接口地址。
        data: {
          login_uid: this.loginUid
        },
        success: (res) => {
          if (res.data.code !== 2000) {
            this.errorMsg = res.data.msg
          } else {
            this.analysisData = res.data.data.analysis_data
            this.loginData = res.data.data.login_data
          }
          console.log(res.data);
        }
      });
    },
    // 确认登录
    confirmLogin() {
      uni.request({
        url: API_URL + 'api/dvadmin_third/confirm/confirm_login/', //仅为示例，并非真实接口地址。
        method: 'post',
        header: {
          'Authorization': 'JWT ' + uni.getStorageSync('token')
        },
        data: {
          login_uid: this.loginUid
        },
        success: (res) => {
          if (res.data.code !== 2000) {
            this.errorMsg = res.data.msg
          } else {
            uni.showToast({
              title: '登录成功！',
            })
            setTimeout(() => {
              if (uni.getStorageSync('is_webview')) {
                webUni.navigateTo({
                  url: '/pages/index/index'
                })
                return
              }
              uni.navigateTo({
                type: 'reLaunch',
                url: '/pages/index/index'
              });
            }, 1500)
          }
          console.log(res.data);
        }
      });
    },
    cancel() {
      if (uni.getStorageSync('is_webview')) {
        webUni.navigateTo({
          url: '/pages/index/index'
        })
        return
      }
      uni.navigateTo({
        type: 'reLaunch',
        url: '/pages/index/index'
      });
    }
  }
}
</script>

<style>
.content {
  /* display: flex; */
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 150rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}

button::after {
  border: none;
}


</style>
